package io.github.eric.urpc.server;

import io.github.eric.urpc.core.rpc.RpcContext;
import io.github.eric.urpc.core.rpc.RpcMethod;
import io.github.eric.urpc.core.rpc.RpcController;
import io.github.eric.urpc.core.rpc.msg.ApplicationInfoReq;
import io.github.eric.urpc.core.rpc.msg.ApplicationInfoRes;
import io.github.eric.urpc.core.rpc.msg.HeartBeat;

public class ServerCoreRpcController implements RpcController {
    private final ServerContext serverContext;

    ServerCoreRpcController(ServerContext serverContext) {
        this.serverContext = serverContext;
    }

    @RpcMethod
    public void onHeartBeat(HeartBeat msg) {
        if (serverContext.getLogger().isDebugEnabled()) {
            serverContext.getLogger().debug("[urpc server] server receive a HeartBeat msg, createdAt: {}, sendAt: {}, traceId: {}",
                    msg.getCreatedAt(), msg.getSendAt(), msg.getTraceId());
        }
    }

    @RpcMethod
    public ApplicationInfoRes onApplicationInfoReq(ApplicationInfoReq applicationInfoReq, RpcContext rpcContext) {
        serverContext.getLogger().info("[urpc server] server receive a register application info req from client {}", rpcContext.getRemoteAddress());
        return serverContext.getConnectionManager().updateConnectionInitStatus(rpcContext.getConnId(), applicationInfoReq);
    }
}
